from django.shortcuts import render
from django.contrib.auth import authenticate, login


def user_login(request):

    # 判断一下请求方式
    # 要是POST请求,就执行登录
    if request.method == 'POST':
        # 获取用户提交的用户名和密码
        user_name = request.POST.get('username', None)
        pass_word = request.POST.get('password', None)
        # 成功返回user对象,失败None
        user = authenticate(username=user_name, password=pass_word)
        # 如果不是null说明验证成功
        if user is not None:
            print("# 登录")
            login(request, user)
            return render(request, 'index.html')
        else:
            return render(request, 'login.html', {'msg': '用户名或密码错误!'})

    # 要GET请求,就加载模板
    elif request.method == 'GET':
        return render(request, 'login.html')
